﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ThirdLife.UI.WinForms.Controls
{
  public partial class TLMenuBar : UserControl, IDockableUserControlView
  {
    private Dictionary<string, IMenuDefinition> _definitionsByName;
    private Dictionary<string, ToolStripMenuItem> _menusByName;
    private SortedList<int, IMenuDefinition> _orderedMenus;
    private Dictionary<string, SortedList<int, ToolStripMenuItem>> _menusItemsByName;

    public TLMenuBar()
    {
      InitializeComponent();
    }

    #region IDockableUserControlView Members

    public void SetupView()
    {
      _definitionsByName = new Dictionary<string, IMenuDefinition>();
      _orderedMenus = new SortedList<int, IMenuDefinition>();
      _menusByName = new Dictionary<string, ToolStripMenuItem>();
      _menusItemsByName = new Dictionary<string, SortedList<int,ToolStripMenuItem>>();
    }

    public void DestroyView()
    {
    }

    public int ControlId
    {
      get { return 0; }
    }

    public object Control
    {
      get { return this; }
    }

    #endregion

    public void CreateMenuFromDefinition(IMenuDefinition menuDefinition)
    {
      string menuName = GetMenuCleanedNameHelper(menuDefinition.Name);
      if (!_definitionsByName.ContainsKey(menuName))
      {
        _definitionsByName.Add(menuName, menuDefinition);
        _orderedMenus.Add(menuDefinition.Order, menuDefinition);
        UpdateMenuBar();
      }
    }

    private void UpdateMenuBar()
    {
      _menusByName = new Dictionary<string, ToolStripMenuItem>();
      menuBar.SuspendLayout();
      menuBar.Items.Clear();
      foreach (KeyValuePair<int, IMenuDefinition> pair in _orderedMenus)
      {
        string menuName = GetMenuCleanedNameHelper(pair.Value.Name);
        ToolStripMenuItem tsmi = new ToolStripMenuItem();
        tsmi.Text = menuName;
        menuBar.Items.Add(tsmi);
        _menusByName.Add(menuName, tsmi);
      }
      //now lets add menu items
      foreach (KeyValuePair<string, SortedList<int, ToolStripMenuItem>> pair in _menusItemsByName)
      {
        string menuName = pair.Key;
        foreach (KeyValuePair<int, ToolStripMenuItem> item in pair.Value)
        {
          _menusByName[menuName].DropDownItems.Add(item.Value);
        }
      }
      menuBar.ResumeLayout();
    }

    public void CreateMenuItemFromDefinition(IMenuItemDefinition menuItemDefinition)
    {
      string menuName = GetMenuCleanedNameHelper(menuItemDefinition.Menu);
      if (_definitionsByName.ContainsKey(menuName))
      {
        SortedList<int, ToolStripMenuItem> menuItemList;
        if( _menusItemsByName.TryGetValue(menuName, out menuItemList) == false )
        {
          menuItemList = new SortedList<int, ToolStripMenuItem>();
          _menusItemsByName.Add(menuName,menuItemList);
        }
        ToolStripMenuItem tsmi = new ToolStripMenuItem(menuItemDefinition.Name);
        //we use an intermediate class, so that we can keep menu click handling pretty abstract
        tsmi.Click += menuItemDefinition.ActivationHandler;
        menuItemList.Add(menuItemDefinition.Order,tsmi);
        UpdateMenuBar();
      }
      else
      {
        throw new ArgumentException("Menu definition was passed in for a menu that does not exist.");
      }
    }

    private string GetMenuCleanedNameHelper(string menu)
    {
      return menu.Trim();
    }
  }
}